import json
import requests
from .base import BaseSpider
from bs4 import BeautifulSoup
import logging
from datetime import datetime
from models.job import Job

class SpiderLLRLZYFW(BaseSpider):
    """老年人力资源服务网爬虫实现"""
    
    def __init__(self, config):
        self.config = config
        self.headers = {**config['headers']}
        self.params = config['params']
    
    def fetch_jobs(self):
        """获取职位列表"""
        try:
            logging.info("正在请求老年人力资源服务网API...")
            
            # 构造请求数据
            data = {
                'current': self.params['current'],
                'size': self.params['size'],
                'cityCode': self.params['cityCode'],
                'jobType': self.params['jobType'],
                'keyword': self.params['keyword'],
                'sortField': '',
                'sortOrder': '',
                'workType': '',
                'salaryMin': '',
                'salaryMax': '',
                'eduLevel': '',
                'workExp': '',
                'ageMin': 60,
                'ageMax': ''
            }
            
            # 添加必要的请求头
            headers = {
                **self.headers,
                'X-Requested-With': 'XMLHttpRequest'
            }
            
            # 尝试获取token（如果需要）
            try:
                token = self._get_token()
                if token:
                    headers['Authorization'] = f'Bearer {token}'
            except Exception as e:
                logging.warning(f"获取token失败: {str(e)}")
            
            # 发送POST请求
            response = requests.post(
                self.config['base_url'],
                headers=headers,
                json=data,
                timeout=10,
                verify=False
            )
            
            logging.info(f"POST请求响应状态码: {response.status_code}")
            logging.debug(f"POST请求头: {headers}")
            logging.debug(f"POST请求数据: {data}")
            logging.debug(f"POST响应内容: {response.text[:200]}...")
            
            response.raise_for_status()
            return response.json()
            
        except requests.exceptions.RequestException as e:
            logging.error(f"API请求失败: {str(e)}")
            return None
        except ValueError as e:
            logging.error(f"JSON解析失败: {str(e)}, 响应内容: {response.text[:200]}")
            return None
        except Exception as e:
            logging.error(f"获取数据失败: {str(e)}")
            return None
    
    def _get_token(self):
        """获取访问令牌"""
        try:
            login_url = 'https://www.llrlzyfw.cn/api/v1/auth/login'
            login_data = {
                'username': 'your_username',  # 需要替换为实际的用户名
                'password': 'your_password'   # 需要替换为实际的密码
            }
            
            response = requests.post(
                login_url,
                headers={
                    'Content-Type': 'application/json',
                    'User-Agent': self.headers['User-Agent']
                },
                json=login_data,
                verify=False
            )
            
            if response.status_code == 200:
                result = response.json()
                return result.get('data', {}).get('token')
                
        except Exception as e:
            logging.error(f"登录失败: {str(e)}")
            
        return None
    
    def parse_job_list(self, data):
        """
        解析职位数据
        
        Args:
            data: API返回的JSON数据
            
        Returns:
            list: Job对象列表
        """
        jobs = []
        if not data or 'data' not in data or 'list' not in data['data']:
            return jobs
            
        for item in data['data']['list']:
            try:
                job = Job(
                    title=item.get('jobName', ''),
                    company=item.get('companyName', ''),
                    salary=f"{item.get('salaryMin', 0)}-{item.get('salaryMax', 0)}元/月",
                    location=item.get('workAddress', ''),
                    description=item.get('jobDesc', ''),
                    contact=item.get('contactPerson', ''),
                    publish_time=datetime.fromtimestamp(item.get('createTime', 0)/1000),
                    source_url=f"https://www.llrlzyfw.cn/#/jobs/detail?id={item.get('id')}"
                )
                
                # 检查年龄要求
                min_age = item.get('ageMin', 0)
                max_age = item.get('ageMax', 999)
                if min_age >= self.config.get('min_age', 60):
                    jobs.append(job)
                    
            except Exception as e:
                logging.error(f"解析职位信息失败: {str(e)}")
                continue
                
        return jobs
    
    def extract_age_requirement(self, description):
        """从API数据中直接获取年龄要求"""
        try:
            # 尝试从API响应中获取年龄要求
            if hasattr(self, '_current_job_data'):
                min_age = self._current_job_data.get('ageMin', 0)
                if min_age > 0:
                    return min_age
                    
            # 如果API中没有，则从描述文本中提取
            import re
            age_patterns = [
                r'(\d{2,3})[岁歲]以上',
                r'年龄(\d{2,3})以上',
                r'(\d{2,3})-\d{2,3}岁',
                r'年龄要求[：:]*(\d{2,3})',
                r'年龄不限|年龄要求不限'
            ]
            
            if any(pattern in description for pattern in ['年龄不限', '年龄要求不限']):
                return 0
                
            for pattern in age_patterns:
                matches = re.findall(pattern, description)
                if matches:
                    try:
                        age = int(matches[0])
                        return age
                    except ValueError:
                        continue
                        
            return None
        except Exception as e:
            logging.error(f"提取年龄要求失败: {str(e)}")
            return None